<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出登录</el-button>
    </el-header>
    <!-- 主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="toggle===true?'64px':'200px'">
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          :unique-opened="true"
          :collapse="toggle"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >
          <div class="toggle-button" @click="toggleCollapse">|||</div>
          <!-- 一级菜单 -->
          <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/'+item1.path" v-for="item1 in item.children"
            :key="item1.id" @click="saveNavState('/'+item1.path)">
              <i class="el-icon-menu"></i>
              <span>{{item1.authName}}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧主体 -->
      <el-main>
          <!-- 路由填充位 -->
          <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      // 左侧菜单数据
      menuList: [],
      // 字体图标
      iconObj: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 折叠动态
      toggle: false,
      // 选中高亮
      activePath: ''
    }
  },
  // 页面加载时
  created () {
    this.getMenuList()
    // 获取session中的activePath值
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 退出登录
    logout () {
      // 清除token
      window.sessionStorage.clear()
      // 跳转
      this.$router.push('/login')
      // 显示退出登录
      this.$message('已退出登录')
    },

    // 获取左侧菜单数据
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // 将获取的数据赋值给menuList
      this.menuList = res.data
      console.log(this.menuList)
    },

    // 折叠侧边栏
    toggleCollapse () {
      this.toggle = !this.toggle
    },
    // 选中菜单高亮显示
    saveNavState (activePath) {
      // 将index值保存到sessionStorage中
      window.sessionStorage.setItem('activePath', activePath)
      // 将点击的菜单index对应的值赋给activePath
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
}
.el-main {
  background-color: #eaedf1;
}
.iconfont{
    margin-right: 10px;
}
//更改el-menu原样式
.el-menu{
    border-right: none;
}
//折叠
.toggle-button{
    background-color: #4a5064;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    line-height: 24px;
    font-size: 10px;
    cursor: pointer;
}
</style>
